﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Abide.UI_Elements
{
    [ToolboxItem(false)]
    public partial class XboxControl : UserControl
    {
        public XboxControl()
        {
            InitializeComponent();
            Program.XboxChanged += Program_XboxChanged;
        }

        void Program_XboxChanged(object sender, EventArgs e)
        {
            if (Program.Xbox.Xbox.Connected)
            {
                connectToolStripMenuItem.Text = Program.Xbox.Xbox.DebugName;
            }
            else
            {
                connectToolStripMenuItem.Text = "Connect";
            }
        }
        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool PrevState = Program.Xbox.Xbox.Connected;

            if (Program.Xbox.Xbox.Connected)
            {
                Program.Xbox.Xbox.Disconnect();
                connectToolStripMenuItem.Text = "Connect";
            }
            else
            {
                try
                {
                    if (debugIpTextBox.Text.ToLower() == "<auto>")
                        Program.Xbox.Xbox.Connect();
                    else
                        Program.Xbox.Xbox.Connect(debugIpTextBox.Text);
                }
                catch (YeloDebug.Exceptions.NoConnectionException)
                { MessageBox.Show("Failed to Connect to Xbox.", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                finally
                {
                    if (Program.Xbox.Xbox.Connected)
                    {
                        connectToolStripMenuItem.Text = Program.Xbox.Xbox.DebugName;
                        Properties.Settings.Default.XboxIp = debugIpTextBox.Text;
                        Properties.Settings.Default.Save();
                    }
                }
            }

            if (PrevState != Program.Xbox.Xbox.Connected)
                Program.ChangeXbox(this);
        }
        private void XboxControl_Load(object sender, EventArgs e)
        {
            if (Program.Xbox.Xbox.Connected)
            {
                connectToolStripMenuItem.Text = Program.Xbox.Xbox.DebugName;
            }
            else
            {
                connectToolStripMenuItem.Text = "Connect";
                debugIpTextBox.Text = Properties.Settings.Default.XboxIp;
            }
        }
    }
}
